<?php

/**
 * This is the model class for table "article".
 *
 * The followings are the available columns in table 'article':
 * @property integer $id
 * @property integer $category_id
 * @property integer $user_id
 * @property string $title
 * @property string $image
 * @property string $short_content
 * @property string $content
 * @property string $create_time
 * @property string $update_time
 * @property integer $is_hot
 * @property integer $status
 */
class Article extends CActiveRecord
{
    const IS_HOT = 1;
    const NOT_HOT = 0;
    const STATUS_ACTIVED = 1;
    const STATUS_INACTIVED = 0;
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'article';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            // Filter
            array('title','filter','filter'=>'trim'),
            array('title','filter','filter'=>'strip_tags'),
            
            // Validate
            array('title','unique','message'=>'{attribute} này đã tồn tại'),
			array('title, content', 'required',
                'message' => '{attribute} không được để trống'),
			array('category_id, user_id, is_hot, status', 'numerical', 'integerOnly'=>true),
			array('title','length','max'=>300,'tooLong'=>'{attribute} không quá 300 kí tự'),
            array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg, jpeg, gif, png, JPG, JPEG, GIF, PNG',
                 'maxSize'=>410000,
                 'message'=>'{attribute} có dạng jpg, jpeg, gif, png',
                 'tooLarge'=>'{attribute} không quá 400KB',
            ),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, is_hot, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'article_category'=>array(self::BELONGS_TO,'ArticleCategory','category_id'),
            'user'=>array(self::BELONGS_TO,'User','user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category_id' => 'Chuyên mục',
			'user_id' => 'Người đăng',
			'title' => 'Tiêu đề',
            'image' => 'Ảnh bài viết',
			'short_content' => 'Nội dung ngắn',
			'content' => 'Nội dung',
			'create_time' => 'Ngày tạo',
			'update_time' => 'Ngày cập nhật',
			'is_hot' => 'Tin hot',
			'status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('short_content',$this->short_content,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('is_hot',$this->is_hot);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'id DESC',
            ),
            'pagination' => array(
                'pageSize' => 40,
            ),
		));
	}
    
    public function getIsHotArray($label = '') {
        if($label) $isHotArray[''] = $label;
        $isHotArray[self::NOT_HOT] = 'Tin thường';
        $isHotArray[self::IS_HOT] = 'Tin hot';
        return $isHotArray;
    }
    
    public function getStatusArray($label = '') {
        if($label) $statusArray[''] = $label;
        $statusArray[self::STATUS_ACTIVED] = 'Đã duyệt';
        $statusArray[self::STATUS_INACTIVED] = 'Chưa duyệt';
        return $statusArray;
    }
    
    public function getImageArticle($image) {
        $imageUrl = Yii::app()->request->baseUrl.'/data/article/';
        if ($image == '0')
            $imageUrl .= 'default-image.jpg';
        else
            $imageUrl .= $image;
        return $imageUrl;
    }
}